﻿using QG.FrontDesk.Core;
using QG.FrontDesk.DAL.IRepository;
using QG.FrontDesk.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QG.FrontDesk.DAL.Repository
{
    public class Position_ResumeinfoResitory : IPosition_ResumeinfoResitory
    {
        /// <summary>
        /// 投递简历
        /// </summary>
        /// <param name="position_Resumeinfo"></param>
        /// <returns></returns>
        public int AddPosition_Resumeinfo(Position_ResumeInfo position_Resumeinfo, int userId)
        {
            string sql = "SELECT rid FROM resumeinfo WHERE UserId=@UserId";
            position_Resumeinfo.RId = DapperDBHelper<ResumeInfo>.Query(sql, new { UserId = userId });
            sql = "INSERT position_resumeinfo(`PostId`,`RId`,`Statu`,`IsLook`,`TIME`) VALUES(@PostId,@RId,1,0,NOW());";
            return DapperDBHelper<Position_ResumeInfo>.Execute(sql, position_Resumeinfo);
        }
        /// <summary>
        /// 简历是否符合要求
        /// </summary>
        /// <param name="position_Resumeinfo"></param>
        /// <returns></returns>
        public bool IsAccord(int postId, int userId)
        {
            string sql = "SELECT * from resumeinfo WHERE RId= (SELECT RId FROM userInfo WHERE userId=@userId)";
            ResumeInfo resumeInfo = DapperDBHelper<ResumeInfo>.QueryFirstOrDefault(sql, new { userId = userId });
            sql = "SELECT * FROM positionInfo WHERE PostId=@PostId";
            PositionInfo position = DapperDBHelper<PositionInfo>.QueryFirstOrDefault(sql, new { PostId = postId });
            //得到简历下的工作经历
            sql = @"SELECT * FROM resumeInfo
                        INNER JOIN businessExperien ON businessExperien.`experId`= resumeInfo.`experId`
                        where resumeInfo.`experId`=@experId";
            resumeInfo.Businessexperien = DapperDBHelper<Businessexperien>.QueryFirstOrDefault(sql, new { experId =resumeInfo.ExperId });
            //得到简历下的教育背景
            sql = @"SELECT * FROM resumeInfo
                    INNER JOIN educational ON educational.`educId`= resumeInfo.`educId`
                    WHERE educational.educId = @educId";
            resumeInfo.Educational = DapperDBHelper<Educational>.QueryFirstOrDefault(sql, new { educId = resumeInfo.EducId });
            //得到简历下的工作性质
            sql = @"SELECT * FROM resumeInfo
                        INNER JOIN ExpectToWork ON expecttowork.`workId`= resumeInfo.`workId`
                        where resumeInfo.`workId`=@workId";
            resumeInfo.Expecttowork = DapperDBHelper<ExpecttoWork>.QueryFirstOrDefault(sql, new { workId = resumeInfo.WorkId });
            if ((resumeInfo.Businessexperien.OverTime.Year - resumeInfo.Businessexperien.BeginTime.Value.Year) < position.WorkNature//工作经验不足的
                || position.Education < resumeInfo.Educational.Education//教育背景不符合的
                || position.WorkNature !=resumeInfo.Expecttowork.WorkNature)//工作性质不符
            {
                return false;
            }
            return true;
            
        }
    }
}
